package com.sora.eduorder.service.impl;

import com.sora.commonutils.vo.CourseWebVoOrder;
import com.sora.commonutils.vo.UcenterMemberVo;
import com.sora.eduorder.client.EduServiceClient;
import com.sora.eduorder.client.UcenterClient;
import com.sora.eduorder.entity.TOrder;
import com.sora.eduorder.mapper.TOrderMapper;
import com.sora.eduorder.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sora.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author sora
 * @since 2023-01-14
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    //远程调用serviceEdu
    @Autowired
    private EduServiceClient eduServiceClient;

    //远程调用serviceUcenter
    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public String createOrders(String courseId, String memberId) {
        //根据用户id，获取用户信息
        UcenterMemberVo memberInfo = ucenterClient.getMemberInfoById(memberId);

        //根据课程id，获取课程信息
         CourseWebVoOrder courseInfo = eduServiceClient.getCourseInfoByIdOrder(courseId);

        TOrder tOrder = new TOrder();
        tOrder.setMail(memberInfo.getMail());
        tOrder.setNickname(memberInfo.getNickname());
        tOrder.setMemberId(memberId);
        tOrder.setCourseCover(courseInfo.getCover());
        tOrder.setCourseId(courseId);
        tOrder.setCourseTitle(courseInfo.getTitle());
        tOrder.setTeacherName(courseInfo.getTeacherName());
        tOrder.setTotalFee(courseInfo.getPrice());
        //支付状态：（ 0：已支付，1：未支付 ）
        tOrder.setStatus(0);
        //支付类型： 1：微信 ， 2：支付宝
        tOrder.setPayType(1);
        //订单号
        tOrder.setOrderNo(OrderNoUtil.getOrderNo());

        //保存订单
        baseMapper.insert(tOrder);

        //返回订单号
        return tOrder.getOrderNo();
    }
}
